package com.gree.scmservice.controller.saas.admin;

import com.gree.common.entity.ReturnVo;
import com.gree.common.entity.TablePageData;
import com.gree.scmservice.entity.saas.SaasSysAddon;
import com.gree.scmservice.entity.saas.vo.SaasSysAddonVo;
import com.gree.scmservice.entity.smbse.SysMenuMo;
import com.gree.scmservice.service.GscmMenuService;
import com.gree.scmservice.service.saas.SaasAddonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "插件信息(管理端)")
@RestController
@RequestMapping("/saas/admin/addon")
public class AdminAddonController {

    @Autowired
    private SaasAddonService saasAddonService;

    @Autowired
    private GscmMenuService gscmMenuService;
    /**
     * 新增插件信息
     */
    @PostMapping
    @ApiOperation(value = "新增插件信息")
    @ApiImplicitParam(name = "saasSysAddon", value = "插件信息", dataType = "SaasSysAddon")
    public ReturnVo add(@RequestBody SaasSysAddon saasSysAddon)
    {
        return ReturnVo.toAjax(saasAddonService.addPlugin(saasSysAddon));
    }

    /**
     * 修改插件信息
     */
    @PutMapping
    @ApiOperation(value = "修改插件信息")
    @ApiImplicitParam(name = "saasSysAddon", value = "插件信息", dataType = "SaasSysAddon")
    public ReturnVo edit(@RequestBody SaasSysAddonVo saasSysAddonVo)
    {
        return ReturnVo.toAjax(saasAddonService.updatePlugin(saasSysAddonVo));
    }

    /**
     * 删除插件信息
     */
    @DeleteMapping("/{addonIds}")
    @ApiOperation(value = "删除插件信息")
    @ApiImplicitParam(name = "addonIds", value = "主键", dataType = "Long[]")
    public ReturnVo remove(@PathVariable Long[] addonIds)
    {
        return ReturnVo.toAjax(saasAddonService.deleteSaasSysAddonByIds(addonIds));
    }

    /**
     *  分页查询插件信息
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询插件信息列表")
    @ApiImplicitParam(name = "saasSysAddon", value = "插件信息", dataType = "SaasSysAddon")
    public ReturnVo<TablePageData<SaasSysAddon>> selectPluginPage(SaasSysAddon saasSysAddon){
        TablePageData<SaasSysAddon> tablePageData = saasAddonService.selectPluginPage(saasSysAddon);
        return ReturnVo.success(tablePageData);
    }

    /**
     * 获取插件信息详细信息
     */
    @GetMapping(value = "/{addonId}")
    @ApiOperation(value = "获取插件信息详细信息")
    @ApiImplicitParam(name = "addonId", value = "主键", dataType = "Long")
    public ReturnVo getInfo(@PathVariable("addonId") Long addonId)
    {
        return ReturnVo.success(saasAddonService.selectById(addonId));
    }

    /**
     * 获取插件信息菜单
     */
    @GetMapping(value = "/roleMenuTreeselect/{addonId}")
    @ApiOperation(value = "获取插件信息菜单")
    @ApiImplicitParam(name = "addonId", value = "主键", dataType = "Long")
    public ReturnVo roleMenuTreeselect(@PathVariable("addonId") Long addonId)
    {
        SysMenuMo sysMenuMo = new SysMenuMo();
        sysMenuMo.setStyp(0);
        sysMenuMo.setParentId(0L);
        ArrayList result = new ArrayList();

        List<SysMenuMo> list = gscmMenuService.selectMenuList(sysMenuMo);
        result.add(gscmMenuService.buildMenuTreeSelect(list));

        sysMenuMo.setStyp(1);
        list = gscmMenuService.selectMenuList(sysMenuMo);
        result.add(gscmMenuService.buildMenuTreeSelect(list));

        sysMenuMo.setStyp(2);
        list = gscmMenuService.selectMenuList(sysMenuMo);
        result.add(gscmMenuService.buildMenuTreeSelect(list));

        sysMenuMo.setStyp(3);
        list = gscmMenuService.selectMenuList(sysMenuMo);
        result.add(gscmMenuService.buildMenuTreeSelect(list));

        sysMenuMo.setStyp(4);
        list = gscmMenuService.selectMenuList(sysMenuMo);
        result.add(gscmMenuService.buildMenuTreeSelect(list));

        NutMap nutMap = NutMap.NEW().addv("checkedKeys", saasAddonService.selectMenuListByAddonId(addonId)).addv("menus", result);
        return new ReturnVo("200","success",nutMap);
    }

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传插件信息图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataType = "MultipartFile"),
            @ApiImplicitParam(name = "addonId", value = "插件ID", dataType = "String")
    })
    public ReturnVo upload(MultipartFile file, String addonId) {
        return saasAddonService.uploadIcon(file, addonId);
    }

    /**
     * 下载文件
     */
    @GetMapping("/download")
    @ApiOperation(value = "下载插件信息图片")
    @ApiImplicitParam(name = "path", value = "插件图片", dataType = "String")
    public void download(@RequestParam String path, HttpServletResponse response) {
        saasAddonService.downloadIcon(path, response);
    }


    /**
     * 插件与菜单绑定
     */
    @PostMapping("/menubind")
    @ApiOperation(value = "插件菜单绑定")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addonId", value = "插件ID", dataType = "Long"),
            @ApiImplicitParam(name = "teno", value = "菜单[]", dataType = "String[]")
    })
    public ReturnVo menuInsertAddUpdate(String addonId, String[] teno){
        int update = saasAddonService.menuInsertAddUpdate(addonId,teno);
        return ReturnVo.toAjax(update);
    }

}
